﻿using MonitorPrism_Practice.Model;
using MonitorPrism_Practice.Mvvm;
using MonitorPrism_Practice.Services;
using MonitorPrism_Practice.Win32;
using S7.Net;
using SCADALight.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;

namespace MonitorPrism_Practice.ViewModels.SettingConfigViewModel
{
    public class AddReciveDataViewModel : BindableBase, IViewLoadedAndUnloadedAware
    {
      
      
        private ReciveDataEventService ReciveDataEvent { get; }
        InterfaceReciveDataViewModel InterfaceReciveDataViewModel;
        public AddReciveDataViewModel(InterfaceReciveDataViewModel interfaceReciveDataViewModel,
            ReciveDataEventService reciveDataEvent,
            IkeyBoardMouseClickService ikeyBoardMouseClickService)
        {
         //   IkeyBoardMouseClickService = ikeyBoardMouseClickService;
            InterfaceReciveDataViewModel = interfaceReciveDataViewModel;
            ReciveDataEvent = reciveDataEvent;
        }

        private string _Name;
        public string Name
        {
            get { return _Name; }
            set
            {
                SetProperty(ref _Name, value);
            }
        }

        private string _address;
        public string Address
        {
            get { return _address; }
            set
            {
                SetProperty(ref _address, value);
            }
        }
        private string _dbIndex;
        public string DbIndex
        {
            get => _dbIndex;
            set { SetProperty(ref _dbIndex, value); }
        }
        private string dbDataIndex;
        public string DbDataIndex
        {
            get => dbDataIndex;
            set { SetProperty(ref dbDataIndex, value); }
        }

        public ObservableCollection<string> _varTypeCollection = new ObservableCollection<string>()
        {
           "Bit","Word","DWord","Int","DInt","Real"

        };
        public ObservableCollection<string> VarTypeCollection
        {
            get => _varTypeCollection;
            set { SetProperty(ref _varTypeCollection, value); }
        }
        public int _varTypeIndex;
        public int VarTypeIndex
        {
            get => _varTypeIndex;
            set { SetProperty(ref _varTypeIndex, value); }
        }

        private ICommand _addItemCommand;
        public ICommand AddItemCommand
        {
            get
            {
                return this._addItemCommand ?? (this._addItemCommand = new DelegateCommand(() =>
                {
                    var values = Enum.GetValues(typeof(ReciveDataType));
                   
                    ReciveDataEvent.AddItem(new Model.InterfaceReciveDataModel()
                    {
                        Name = this.Name,
                        Address = this.Address ??= $"DBD{this.DbIndex}.DB{values.GetValue(VarTypeIndex)}{this.dbDataIndex}",
                        DataType = (VarType)values.GetValue(VarTypeIndex),
                        DbIndex = int.Parse(this.DbIndex),
                        DbDataIndex = int.Parse(this.dbDataIndex)
                    });
                })); 
            }
        }



        private ICommand _ancellCommand;
        public ICommand CancellCommand
        {
            get
            {
                return this._ancellCommand ?? (this._ancellCommand = new DelegateCommand(() =>
                {
                    this.Name = null;
                    this.Address = null;
                    this.DbIndex = null;
                    this.DbIndex = null;
                }));
            }
        }
        

        public void OnLoaded(object sender, RoutedEventArgs e)
        {
          
        }
      

        public void OnUnloaded(object sender, RoutedEventArgs e)
        {

        }


    }
    public enum ReciveDataType
    {
        Bit = VarType.Bit,
        Word = VarType.Word,
        DWord = VarType.DWord,
        Int = VarType.Int,
        DInt = VarType.DInt,
        Real = VarType.Real,
    }
}
